const Router = require('koa-router')
const router = new Router()
const u = require('../api')
const title = '元翔动态详情_新闻中心详情_元翔职校'
const description = '新闻中心详情，元翔动态详情，元翔职校'
const keyword = '元翔动态详情,新闻中心详情,元翔职校'

router.get('/yxdt/:id.html', async (ctx) => {
  ctx.response.status = 200
  const id = ctx.params.id || ''
  ctx.config.nav[2].checked = true
  ctx.config.title = title
  ctx.config.description = description
  ctx.config.keyword = keyword
  ctx.config.banner = `${ctx.config.host}/imgs/banner3-1.jpg`

  let { errno, errmsg, result } = await u.getNewsInfo({
    id,
    upAndDown: true
  })
  if (errno == 0 && result) {
    ctx.config.title = result.title
    ctx.config.description = result.title
    ctx.config.keyword = result.title
    result.titleNow = result.title.length > 8 ? result.title.substring(0, 8) + '...' : result.title
  } else {
    ctx.redirect('/404.html') 
  }

  await ctx.render('xwzxxq.ejs', {
    config: ctx.config,
    menu: [{
      title: '元翔动态',
      pinyin: 'yxdt',
      checked: true
    }, {
      title: '行业动态',
      pinyin: 'hydt',
      checked: false
    }],
    pathname: 'yxdt',
    infoData: result || null
  })
})

router.get('/hydt/:id.html', async (ctx) => {
  ctx.response.status = 200
  const id = ctx.params.id || ''
  ctx.config.nav[2].checked = true
  ctx.config.title = title
  ctx.config.description = description
  ctx.config.keyword = keyword
  ctx.config.banner = `${ctx.config.host}/imgs/banner3-1.jpg`

  let { errno, errmsg, result } = await u.getNewsInfo({
    id,
    upAndDown: true
  })
  if (errno == 0 && result) {
    ctx.config.title = result.title
    ctx.config.description = result.title
    ctx.config.keyword = result.title
    result.titleNow = result.title.length > 8 ? result.title.substring(0, 8) + '...' : result.title
  } else {
    ctx.redirect('/404.html') 
  }

  await ctx.render('xwzxxq.ejs', {
    config: ctx.config,
    menu: [{
      title: '元翔动态',
      pinyin: 'yxdt',
      checked: false
    }, {
      title: '行业动态',
      pinyin: 'hydt',
      checked: true
    }],
    pathname: 'hydt',
    infoData: result || null
  })
})

module.exports = router